package com.zxmr.hospital.customer.action;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.Contract;
import com.zxmr.domain.ContractStrategy;
import com.zxmr.domain.Opportunity;
import com.zxmr.domain.service.ContractService;
import com.zxmr.domain.service.OpportunityService;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.UserTraceManager;
import com.zxmr.util.Utils;

public class ContractAction extends Executor {

	enum method{
		add,
		update,
		query,
		queryAll;
	}
	@Override
	public void execute(Request request, Response response) {
		
		try
		{
			method m = Enum.valueOf(method.class, this.getElement().getMethod());
			switch(m)
			{
			case add:
				add(request,response);
				break;
			case update:
				update(request,response);
				break;
			case query:
				query(request,response);
				break;
			case queryAll:
				break;
			}
		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue(ResourceManager.getInstance().getMessage("com.zxmr.customer.internalError"));
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
	}

	private void add(Request request,Response response)throws ExecuteException
	{
		Contract contract = parseAddParameter(request);
		validateAddParameter(contract,request);
		setFixParameterForAdd(request,contract);
		
		ContractService service = new ContractService();
		if(!service.addContract(contract))
		{
			this.internalErrorResponse(response);
		}else
		{
			// send action trace
			String action = ResourceManager.getInstance().getMessage("com.zxmr.customer.addContract",contract.getSequence(),contract.getCustomerName());
			UserTraceManager.addTrace(action, request.getAccount());
		}
		
	}
	private void setFixParameterForAdd(Request request,Contract contract)
	{
		contract.setCompany(request.getAccount().getCompany());
		contract.setCreateTime(Utils.getCurrentTime());
		contract.setCreateUser(request.getAccount().getName());
		contract.setSequence(String.valueOf(System.currentTimeMillis()));
	}
	private void validateAddParameter(Contract contract,Request request)throws ExecuteException
	{
		if(contract == null)
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.errorPara"));
		long opportunity = contract.getOpportunity();
		if(opportunity < 1)
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.noOpportunity"));
		OpportunityService oservice = new OpportunityService();
		Opportunity opp = oservice.queryOpportunity(opportunity);
		if(opp == null || opp.getCompany() != request.getAccount().getCompany())
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.errorPara"));
		
		contract.setCustomer(opp.getCustomer());
		contract.setCustomerName(opp.getCustomerName());
		
		if(contract.getStrategy() == null)
			contract.setStrategy(ContractStrategy.processing);
	}
 	private Contract parseAddParameter(Request request)throws ExecuteException
 	{
 		try
 		{
 			return JSON.parseObject(request.getRequestParameters().get("parameters"), Contract.class);
 		}catch(Exception e)
 		{
 			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.errorPara"));
 		}
 	}
 	private void query(Request request,Response response)throws ExecuteException
 	{
 		int id = 0;
 		try
 		{
 			id = Integer.parseInt(request.getRequestParameters().get("id"));
 		}catch(Exception e)
 		{
 			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.errorPara"));
 		}
 		
 		ContractService service = new ContractService();
 		Contract contract = service.queryContract(id,request.getAccount().getCompany());
 		if(contract == null)
 			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.contract.notexist"));
 		
 		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(contract));
 		response.setPayload(payload);
 	}
 	
	private void update(Request request,Response response)throws ExecuteException
	{
		Contract contract = parseAddParameter(request);
		ContractService service = new ContractService();
		Contract orign = service.queryContract(contract.getId(), request.getAccount().getCompany());
		
		if(orign == null) {
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.contract.notexist"));
		}
		
		if(contract.getOpportunity() <= 0) {
			contract.setOpportunity(orign.getOpportunity());
		}
		if(Utils.isStringEmptyWithTrim(contract.getContractAttachment())) {
			contract.setContractAttachment(orign.getContractAttachment());
		}
		
		validateAddParameter(contract,request);
		
		if(!service.updateContract(contract))
		{
			this.internalErrorResponse(response);
		}
		
	}
}
